package handler

import (
	"car-trade-hub/model"
	"github.com/gin-gonic/gin"
	"strconv"
)

func UsersRoles(c *gin.Context) {
	rid, _ := strconv.Atoi(c.PostForm("rid"))
	uid, _ := strconv.Atoi(c.PostForm("uid"))
	data := model.UsersRole{
		Uid: uid,
		Rid: rid,
	}
	model.DB.Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "成功",
	})
}

func UserRole(c *gin.Context) {
	/*
		SELECT
		*
		FROM
		users
		LEFT JOIN roles  ON roles.uid=users.id
	*/
	id, _ := strconv.Atoi(c.Query("id"))
	var userrole []model.Role
	model.DB.Table("roles").Joins("LEFT JOIN users  ON roles.uid=users.id").Where("roles.uid=?", id).Debug().Find(&userrole)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "成功",
		"data": userrole,
	})
	return
}
func RoleNode(c *gin.Context) {
	/*
		SELECT
		*
		FROM
		users
		LEFT JOIN roles  ON roles.uid=users.id
	*/
	id, _ := strconv.Atoi(c.Query("id"))
	var rolenode []model.Node
	model.DB.Table("nodes").Joins("LEFT JOIN roles  ON roles.nid=nodes.id").Where("nodes.id=?", id).Debug().Find(&rolenode)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "成功",
		"data": rolenode,
	})
	return
}
